package com.honour.busserver.backup.job;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.honour.busserver.backup.config.WebSocket;
import com.honour.busserver.backup.config.WebSocketConfig;
import com.honour.busserver.backup.entity.BusBackConfig;
import com.honour.busserver.backup.entity.BusBackFile;
import com.honour.busserver.backup.mapper.BusBackConfigMapper;
import com.honour.busserver.backup.service.BusBackConfigService;
import com.honour.busserver.backup.service.BusBackFileService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.JobListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MyJobListener implements JobListener {

    @Autowired
    private BusBackFileService busBackFileService;
    @Autowired
    private BusBackConfigMapper busBackConfigMapper;
    @Autowired
    private WebSocket webSocket;
    @Override
    public String getName() {
        return "MyJobListener";
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
        System.out.println("Job监听器：MyJobListener.jobToBeExecuted()");
        JobKey key = jobExecutionContext.getJobDetail().getKey();
        //查询库名
        QueryWrapper<BusBackConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("job_name",key.getName());
        BusBackConfig busBackConfig = busBackConfigMapper.selectOne(wrapper);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//        String fileName = dateFormat.format(new Date())+"_"+busBackConfig.getDb()+"_backup.ora";

        BusBackFile busBackFile = new BusBackFile();
        busBackFile.setStatus("0");
        busBackFile.setJobName(key.getName());
        busBackFile.setFileName("fileName");
        busBackFile.setTime(new Date());
        //保存文件信息
        busBackFileService.save(busBackFile);

        //webSocket发送数据
        webSocket.sendAllMessage(JSON.toJSONString(busBackFile));

    }


    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {
        System.out.println("Job监听器：MyJobListener.jobExecutionVetoed()");

    }

    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
        System.out.println("Job监听器：MyJobListener.jobWasExecuted()");
        JobKey key = jobExecutionContext.getJobDetail().getKey();
        BusBackFile busBackFile = new BusBackFile();
        busBackFile.setStatus("1");
        busBackFile.setJobName(key.getName());
        UpdateWrapper<BusBackFile> wrapper = new UpdateWrapper<>();
        wrapper.eq("job_name",key.getName());
        wrapper.set("status","1");
        //更新数据状态
        busBackFileService.update(wrapper);
        //根据jobName查询数据id
//        QueryWrapper<BusBackFile> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("job_name",key.getName());
//        BusBackFile temp_file = busBackFileService.getOne(queryWrapper);
        busBackFile.setId(2459);
        //webSocket发送数据
        webSocket.sendAllMessage(JSON.toJSONString(busBackFile));

    }
}
